
/*
 * PanelMoneda.java
 *
 * Created on 25/01/2013, 03:36:22 PM
 */
package elaprendiz.ventanas.paneles;

import elaprendiz.controllers.CMoneda;
import elaprendiz.modelbd.Moneda;
import elaprendiz.modelgui.ListModelMoneda;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Ferz
 */
public class PanelMoneda extends javax.swing.JPanel {

    /** Creates new form PanelMoneda */
    DefaultListModel mdlMoneda;
    CMoneda cm;
    Moneda seleccionada;
    public PanelMoneda() {
        initComponents();
        cm = new CMoneda();
        mdlMoneda = new ListModelMoneda();
        lstMonedas.setModel(mdlMoneda);
        lstMonedas.setCellRenderer(new ListCellRenderer(){
            protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                JLabel renderer = (JLabel)defaultRenderer.getListCellRendererComponent(
                                    list, value, index, isSelected, cellHasFocus);
                Moneda mnd = (Moneda)value;
                if(mnd.getActivoBool())
                {
                   renderer.setIcon(new ImageIcon(this.getClass().getResource("/elaprendiz/resources/tick.png"))); 
                }else{
                    renderer.setIcon(new ImageIcon(this.getClass().getResource("/elaprendiz/resources/cancel.png"))); 
                }
                
                if(mnd.getPredeterminadoBool())
                {
                   renderer.setIcon(new ImageIcon(this.getClass().getResource("/elaprendiz/resources/predeterminado.png")));  
                }
                return renderer;
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelImage1 = new elaprendiz.gui.panel.PanelImage();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        bntGrabar = new elaprendiz.gui.button.ButtonRect();
        bntModificar = new elaprendiz.gui.button.ButtonRect();
        bntEliminar = new elaprendiz.gui.button.ButtonRect();
        bntCancelar = new elaprendiz.gui.button.ButtonRect();
        bntSalir = new elaprendiz.gui.button.ButtonRect();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new elaprendiz.gui.textField.TextField();
        jLabel2 = new javax.swing.JLabel();
        txtSimbolo = new elaprendiz.gui.textField.TextField();
        chbEstado = new javax.swing.JCheckBox();
        chbDefault = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstMonedas = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/fondoazulceleste.jpg"))); // NOI18N
        panelImage1.setLayout(new javax.swing.BoxLayout(panelImage1, javax.swing.BoxLayout.LINE_AXIS));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setOpaque(false);

        bntGrabar.setBackground(new java.awt.Color(51, 153, 255));
        bntGrabar.setText("Grabar");
        bntGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntGrabarActionPerformed(evt);
            }
        });
        jPanel3.add(bntGrabar);

        bntModificar.setBackground(new java.awt.Color(51, 153, 255));
        bntModificar.setText("Modificar");
        bntModificar.setEnabled(false);
        bntModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntModificarActionPerformed(evt);
            }
        });
        jPanel3.add(bntModificar);

        bntEliminar.setBackground(new java.awt.Color(51, 153, 255));
        bntEliminar.setText("Eliminar");
        bntEliminar.setEnabled(false);
        bntEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntEliminarActionPerformed(evt);
            }
        });
        jPanel3.add(bntEliminar);

        bntCancelar.setBackground(new java.awt.Color(51, 153, 255));
        bntCancelar.setText("Cancelar");
        bntCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarActionPerformed(evt);
            }
        });
        jPanel3.add(bntCancelar);

        bntSalir.setBackground(new java.awt.Color(51, 153, 255));
        bntSalir.setText("Salir");
        jPanel3.add(bntSalir);

        jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_END);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setOpaque(false);
        jPanel4.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Nombre De La Moneda:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel4.add(jLabel1, gridBagConstraints);

        txtNombre.setFont(new java.awt.Font("Arial", 0, 12));
        txtNombre.setPreferredSize(new java.awt.Dimension(200, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel4.add(txtNombre, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Simbolo:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel4.add(jLabel2, gridBagConstraints);

        txtSimbolo.setFont(new java.awt.Font("Arial", 0, 12));
        txtSimbolo.setPreferredSize(new java.awt.Dimension(80, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel4.add(txtSimbolo, gridBagConstraints);

        chbEstado.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        chbEstado.setSelected(true);
        chbEstado.setText("Activo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 36, 0, 2);
        jPanel4.add(chbEstado, gridBagConstraints);

        chbDefault.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        chbDefault.setText("Predeterminado");
        chbDefault.setEnabled(false);
        chbDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbDefaultActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel4.add(chbDefault, gridBagConstraints);

        jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER);

        panelImage1.add(jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setOpaque(false);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(120, 130));

        lstMonedas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstMonedas.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstMonedasValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstMonedas);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        panelImage1.add(jPanel2);

        add(panelImage1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void bntGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntGrabarActionPerformed
    if(!txtNombre.getText().isEmpty() &&  !txtSimbolo.getText().isEmpty())
    {
        int op = JOptionPane.showConfirmDialog(null, "¿Desea Guardar la moneda "
                + "con el nombre"+txtNombre.getText()+" y simbolo "
                + txtSimbolo.getText()+" ?", "¿Desea agregar Moneda Nueva?", JOptionPane.YES_NO_CANCEL_OPTION);
        if(op == JOptionPane.OK_OPTION)
        {
            Moneda moneda = new Moneda();
            moneda.setNombre(txtNombre.getText());
            moneda.setSimbolo(txtSimbolo.getText());
           boolean rsp = cm.guardarRegistro(moneda);
           if(rsp)
           {
               JOptionPane.showMessageDialog(null, "Moneda nueva agregada");
               mdlMoneda = new ListModelMoneda();
               lstMonedas.setModel(mdlMoneda);
               txtNombre.setText("");
               txtSimbolo.setText("");
           }else{
               JOptionPane.showMessageDialog(null, "No se pudo guardar");
           }
        }
    }
}//GEN-LAST:event_bntGrabarActionPerformed

private void bntModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntModificarActionPerformed
    if(!txtNombre.getText().isEmpty() &&  !txtSimbolo.getText().isEmpty())
    {
        if(!seleccionada.getNombre().equals(txtNombre.getText()) || !seleccionada.getSimbolo().equals(txtSimbolo.getText())||
                chbEstado.isSelected() != seleccionada.getActivoBool())
        {
          int op = JOptionPane.showConfirmDialog(null, "¿Desea Cambiar la moneda "
                + seleccionada.getNombre()+" por "+txtNombre.getText()+" ?", "¿Desea Modifica Moneda?", JOptionPane.YES_NO_CANCEL_OPTION);
            if(op == JOptionPane.OK_OPTION)
            {  
                 seleccionada.setNombre(txtNombre.getText());
                  seleccionada.setSimbolo(txtSimbolo.getText());
                  if(chbEstado.isSelected())
                  {
                     seleccionada.setActivo(1); 
                  }else
                  {
                     seleccionada.setActivo(0); 
                  }
               int rsp = cm.actualizarRegistro(seleccionada);
               if(rsp != 0)
               {
                   JOptionPane.showMessageDialog(null, "Moneda Actualizada");
                   mdlMoneda = new ListModelMoneda();
                   lstMonedas.setModel(mdlMoneda);
                   txtNombre.setText("");
                   txtSimbolo.setText("");
                   lstMonedas.clearSelection();
                   bntModificar.setEnabled(false);
                   bntEliminar.setEnabled(false);
                   seleccionada = null;
               }else{
                   JOptionPane.showMessageDialog(null, "No se pudo Actualizar");
               }
            }  
        }else{
            JOptionPane.showMessageDialog(null, "Para modificar debe ingresar un valor diferente al que ya existe");
        }
        
    }
}//GEN-LAST:event_bntModificarActionPerformed

private void lstMonedasValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstMonedasValueChanged
     
    if(lstMonedas.getSelectedIndex() != -1)
  {
      seleccionada = (Moneda)lstMonedas.getSelectedValue();
      this.txtNombre.setText(seleccionada.getNombre());
      this.txtSimbolo.setText(seleccionada.getSimbolo());
      chbEstado.setSelected(seleccionada.getActivoBool());
      if(!seleccionada.getActivoBool())
      {
          chbDefault.setEnabled(false);
      }else
      {
          chbDefault.setEnabled(true);
      }
      if(seleccionada.getPredeterminadoBool())
      {
          chbEstado.setEnabled(false);
          chbDefault.setEnabled(false);
          this.bntEliminar.setEnabled(false);
           this.bntModificar.setEnabled(false);
      }else
      {
          chbEstado.setEnabled(true);
          this.bntEliminar.setEnabled(true);
           this.bntModificar.setEnabled(true);
      }
      chbDefault.setSelected(seleccionada.getPredeterminadoBool());
      
  }
  
}//GEN-LAST:event_lstMonedasValueChanged

private void bntEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntEliminarActionPerformed
    if(lstMonedas.getSelectedIndex() != -1)
  {
      seleccionada = (Moneda)lstMonedas.getSelectedValue();
      int op = JOptionPane.showConfirmDialog(null, "¿Desea Eliminar la moneda "
                + seleccionada.getNombre()+" ?", "¿Eliminar Moneda?", JOptionPane.YES_NO_CANCEL_OPTION);
      if(op == JOptionPane.OK_OPTION)
      {
        int borrarRegistro = cm.borrarRegistro(seleccionada);
        if(borrarRegistro != 0)
        {
            JOptionPane.showMessageDialog(null, "Moneda Eliminada");
               mdlMoneda = new ListModelMoneda();
               lstMonedas.setModel(mdlMoneda);
               txtNombre.setText("");
               txtSimbolo.setText("");
               lstMonedas.clearSelection();
               bntModificar.setEnabled(false);
               bntEliminar.setEnabled(false);
               seleccionada = null;
        }
      }
  }
}//GEN-LAST:event_bntEliminarActionPerformed

private void chbDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbDefaultActionPerformed
  
   if(chbDefault.isSelected()) 
   {
      Moneda pre = (Moneda)lstMonedas.getSelectedValue();
      if(pre != null)
      {
         if(!pre.getPredeterminadoBool())
         {
             int op = JOptionPane.showConfirmDialog(null, "¿Desea establecer esta moneda "
                + "como predeterminada", "Establecer como predeterminada", 
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
              if(op == JOptionPane.OK_OPTION)
              {
                  ListModelMoneda lmm = (ListModelMoneda)lstMonedas.getModel();
                 if(lmm.getPredeterminado() != null)
                  {
                     System.out.println("no es nullllll");
                    lmm.getPredeterminado().setPredeterminado(0);
                    cm.actualizarRegistro(lmm.getPredeterminado());  
                  }
                 pre.setPredeterminado(1);
                 int actualizarRegistro = cm.actualizarRegistro(pre);
                 if(actualizarRegistro != 0)
                 {
                     JOptionPane.showMessageDialog(null, "La moneda: "+pre.getNombre()+
                             "\ncon simbolo: "+pre.getSimbolo()+"\nSe establecio como"
                             + "predeterminada");
                     mdlMoneda = new ListModelMoneda();
                     lstMonedas.setModel(mdlMoneda);
                     chbEstado.setEnabled(false);
                      chbDefault.setEnabled(false);
                      this.bntEliminar.setEnabled(false);
                       this.bntModificar.setEnabled(false);
                 }
              }else{
                  chbDefault.setSelected(false);
              } 
         }
      }  
   }
  
}//GEN-LAST:event_chbDefaultActionPerformed

private void bntCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarActionPerformed
 this.lstMonedas.clearSelection();
 chbEstado.setSelected(true);
 chbDefault.setSelected(false);
 chbEstado.setEnabled(false);
 chbDefault.setEnabled(false);
 txtNombre.setText("");
 txtSimbolo.setText("");
}//GEN-LAST:event_bntCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaprendiz.gui.button.ButtonRect bntCancelar;
    private elaprendiz.gui.button.ButtonRect bntEliminar;
    private elaprendiz.gui.button.ButtonRect bntGrabar;
    private elaprendiz.gui.button.ButtonRect bntModificar;
    private elaprendiz.gui.button.ButtonRect bntSalir;
    private javax.swing.JCheckBox chbDefault;
    private javax.swing.JCheckBox chbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstMonedas;
    private elaprendiz.gui.panel.PanelImage panelImage1;
    private elaprendiz.gui.textField.TextField txtNombre;
    private elaprendiz.gui.textField.TextField txtSimbolo;
    // End of variables declaration//GEN-END:variables
}
